package com.android.fragmentdemo.leanback
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.leanback.widget.Presenter
import com.android.fragmentdemo.R
import com.hengfoxwolf.commonbase.base.databeans.SlipItem
import com.android.fragmentdemo.databinding.ItemHgvLayoutBinding
import com.android.fragmentdemo.databinding.ItemVgvLayoutBinding

class HGVPresenter(context: Context) : Presenter() {

    private val mContext = context

    override fun onCreateViewHolder(parent: ViewGroup?): ViewHolder {
        val mBinding = DataBindingUtil.inflate<ItemHgvLayoutBinding>(
            LayoutInflater.from(mContext),
            R.layout.item_hgv_layout, parent, false

        )
        return HGVViewHolder(mBinding).also { it.mBinding = mBinding }
    }

    override fun onBindViewHolder(viewHolder: ViewHolder?, item: Any?) {
        //Log.d(TAG, "override fun onBindViewHolder")
        if (viewHolder != null) {
            val mBinding : ItemHgvLayoutBinding = (viewHolder as HGVViewHolder).mBinding as ItemHgvLayoutBinding
            val dataItem = item as? SlipItem
            dataItem?.apply {
                mBinding.hgvClRoot.setBackgroundColor(mContext.resources.getColor(R.color.color_C6FF00))
                mBinding.hgvTvContent.text = "${dataItem.number}"
            }

            //Log.d(TAG, "val dataItem = item as? SlipItem")
        }
    }

    override fun onUnbindViewHolder(viewHolder: ViewHolder?) {

    }

    class HGVViewHolder(binding: ViewDataBinding) : ViewHolder(binding.root) {
       lateinit var mBinding: ViewDataBinding
    }

    companion object {
        const val TAG = "HGVPresenter"
    }
}